package com.ruoyi.web.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.file.utils.MinioTool;
import com.ruoyi.web.service.IMinioService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Service
public class MinioServiceImpl implements IMinioService {
    @Resource
    private MinioTool minioTool;
    @Value(value = "${minio.default-bucket}")
    private String defaultBucket;
    @Value(value = "${minio.endpoint}")
    private String endpoint;

    @Override
    public AjaxResult uploadMinio(MultipartFile file) {
        try {
            String url = minioTool.uploadMinio(file);
            AjaxResult ajax = AjaxResult.success();
            //存入数据库

            ajax.put("url", url);
            ajax.put("fileName", url);
            ajax.put("newFileName", FileUtils.getName(url));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
